﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace WPFSample.PrintWithMarginsSample
{
    class PageMarginsDialog : Window
    {
        enum Slide
        {
            Left,
            Right,
            Top,
            Bottom
        }

        TextBox[] txtBox = new TextBox[4];
        Button btnOK;

        public Thickness PageMargins
        {
            get
            {
                return new Thickness(
                    double.Parse(this.txtBox[(int)Slide.Left].Text),
                    double.Parse(this.txtBox[(int)Slide.Top].Text),
                    double.Parse(this.txtBox[(int)Slide.Right].Text),
                    double.Parse(this.txtBox[(int)Slide.Bottom].Text));
            }

            set
            {
                this.txtBox[(int)Slide.Left].Text = value.Left.ToString("F3");
                this.txtBox[(int)Slide.Right].Text = value.Right.ToString("F3");
                this.txtBox[(int)Slide.Top].Text = value.Top.ToString("F3");
                this.txtBox[(int)Slide.Bottom].Text = value.Bottom.ToString("F3");
            }
        }

        public PageMarginsDialog()
        {
            this.Title = "Page Setup";
            this.ShowInTaskbar = false;
            this.WindowStyle = WindowStyle.ToolWindow;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.SizeToContent = SizeToContent.WidthAndHeight;
            this.ResizeMode = ResizeMode.NoResize;

            StackPanel stack = new StackPanel();
            this.Content = stack;

            GroupBox grpBox = new GroupBox();
            grpBox.Header = "Margins";
            grpBox.Margin = new Thickness(12);
            stack.Children.Add(grpBox);

            Grid grid = new Grid();
            grid.Margin = new Thickness(6);
            grpBox.Content = grid;

            for (int i = 0; i < 2; i++)
            {
                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = GridLength.Auto;
                grid.RowDefinitions.Add(rowdef);
            }

            for (int i = 0; i < 4; i++)
            {
                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = GridLength.Auto;
                grid.ColumnDefinitions.Add(coldef);
            }

            for (int i = 0; i < 4; i++)
            {
                Label lbl = new Label
                {
                    Content = "_" + Enum.GetName(typeof(Slide), i) + ":",
                    Margin = new Thickness(6),
                    VerticalAlignment = VerticalAlignment.Center
                };
                grid.Children.Add(lbl);
                Grid.SetRow(lbl, i / 2);
                Grid.SetColumn(lbl, 2 * (i % 2));

                this.txtBox[i] = new TextBox();
                this.txtBox[i].TextChanged += this.TextBoxOnTextChanged;
                this.txtBox[i].MinWidth = 48;
                this.txtBox[i].Margin = new Thickness(6);
                grid.Children.Add(this.txtBox[i]);
                Grid.SetRow(this.txtBox[i], i / 2);
                Grid.SetColumn(this.txtBox[i], 2 * (i % 2) + 1);
            }

            UniformGrid unigrid = new UniformGrid();
            unigrid.Rows = 1;
            unigrid.Columns = 2;
            stack.Children.Add(unigrid);

            this.btnOK = new Button()
            {
                Content = "OK",
                IsDefault = true,
                IsEnabled = false,
                MinWidth = 60,
                Margin = new Thickness(12),
                HorizontalAlignment = HorizontalAlignment.Center
            };
            this.btnOK.Click += this.OKButtonOnClick;
            unigrid.Children.Add(this.btnOK);

            Button btnCancel = new Button()
            {
                Content = "Cancel",
                IsCancel = true,
                MinWidth = 60,
                Margin = new Thickness(12),
                HorizontalAlignment = HorizontalAlignment.Center
            };
            unigrid.Children.Add(btnCancel);
        }

        private void OKButtonOnClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void TextBoxOnTextChanged(object sender, TextChangedEventArgs e)
        {
            double result;
            this.btnOK.IsEnabled = double.TryParse(this.txtBox[(int)Slide.Left].Text, out result) &&
                double.TryParse(this.txtBox[(int)Slide.Right].Text, out result) &&
                double.TryParse(this.txtBox[(int)Slide.Top].Text, out result) &&
                double.TryParse(this.txtBox[(int)Slide.Bottom].Text, out result);
        }
    }
}
